#ifndef __SEAMLESSMAP_H__
#define __SEAMLESSMAP_H__

// Includes
#include <PA9.h>       // Include for PA_Lib
#include "efs_lib.h"

#include "Init.h"
#include "Monster.h"

int incX = 14, incY = 14;

void miniMap()
{
	currentX = x;
	currentY = y;
	
	if(currentX > lastX)
	{
		dotX += (currentX - lastX);
		//if(dotX%12 == 0)
		if((dotX - oldDotX) >= incX)
		{
			PA_SetSpriteX(1, 63, (PA_GetSpriteX(1, 63) + 1));
			oldDotX += incX;
		}
		
	}
	if(currentX < lastX)
	{
		dotX -= (lastX - currentX);
		//if(dotX%12 == 0)
		if((dotX - oldDotX) <= 0)
		{
			PA_SetSpriteX(1, 63, (PA_GetSpriteX(1, 63) - 1));
			oldDotX -= incX;
		}
	}
	
	if(currentY > lastY)
	{
		dotY += (currentY - lastY);
		//if(dotY%12 == 0)
		if((dotY - oldDotY) >= incY)
		{
			PA_SetSpriteY(1, 63, (PA_GetSpriteY(1, 63) + 1));
			oldDotY += incY;
		}
	}
	if(currentY < lastY)
	{
		dotY -= (lastY - currentY);
		//if(dotY%12 == 0)
		if((dotY - oldDotY) <= 0)
		{
			PA_SetSpriteY(1, 63, (PA_GetSpriteY(1, 63) - 1));
			oldDotY -= incY;
		}
	}
	
	
	lastX = currentX;
	lastY = currentY;
}


void scrollControl()
{	
PA_SetSpriteX(0, 6, chest1X - x - scrollOffsetX);
PA_SetSpriteY(0, 6, chest1Y - y - scrollOffsetY);

//PA_SetSpriteX(0, 40, bg[0].getx() - x - scrollOffsetX);
//PA_SetSpriteY(0, 40, bg[0].gety() - y - scrollOffsetY);
int n = 0;

	for(int i = monSprStart; i < (monSprStart + monSize); i++)
	{
		PA_SetSpriteX(0, i, bg[n]->getx() - x - scrollOffsetX);
		PA_SetSpriteY(0, i, bg[n]->gety() - y - scrollOffsetY);
		
		n++;
	}
	
	//scroll vertical and horizontal
	if(scrCtrl == 'b')
	{
		PA_LargeScrollXY(0, 1, x - scrollOffsetX, y - scrollOffsetY);
		PA_LargeScrollXY(0, 2, x - scrollOffsetX, y - scrollOffsetY);
		oldX = x;
		oldY = y;
		
		spriteX = x - oldX;
		spriteY = y - oldY;
	}
	//stop vertical scrolling -- horizontal only
	else if (scrCtrl == 'h')
	{
		spriteX = x - oldX;
		spriteY = y - oldY;
		
		
		PA_SetSpriteXY(0, avSprNo, spriteX + sprStartX , spriteY + sprStartY);	// set sprite position
		PA_LargeScrollXY(0, 1, x - scrollOffsetX, oldY - scrollOffsetY);
		PA_LargeScrollXY(0, 2, x - scrollOffsetX, oldY - scrollOffsetY);
		oldX = x;
	}
	//stop horizontal scrolling -- vertical only
	else if (scrCtrl == 'v')
	{
		spriteX = x - oldX;
		spriteY = y - oldY;
		
		
		PA_SetSpriteXY(0, avSprNo, spriteX + sprStartX , spriteY + sprStartY);	// set sprite position
		PA_LargeScrollXY(0, 1, oldX - scrollOffsetX, y - scrollOffsetY);
		PA_LargeScrollXY(0, 2, oldX - scrollOffsetX, y - scrollOffsetY);
		oldY = y;
	}
	//stop horizontal AND vertical scrolling
	else if (scrCtrl == 'n')
	{
		spriteX = x - oldX;
		spriteY = y - oldY;
		
		
		PA_SetSpriteXY(0, avSprNo, spriteX + sprStartX , spriteY + sprStartY);	// set sprite position
		PA_LargeScrollXY(0, 1, oldX - scrollOffsetX, oldY - scrollOffsetY);
		PA_LargeScrollXY(0, 2, oldX - scrollOffsetX, oldY - scrollOffsetY);
	}
}



void mapScrolling()
{
	switch(map)
	{
	case 10:
		if((x <= 730) && (x >= 10))
		{
			//scroll both directions
			scrCtrl = 'b';
			scrollControl();
		}
		else if(x > 730) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		else if(x < 10) //stop background scroll - horizontal
		{
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		break;
	case 11:
		if((x <= 850) && (x >= 130))
		{
			//scroll both directions
			scrCtrl = 'b';
			scrollControl();
		}
		else if(x > 850) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		else if(x < 130) //stop background scroll - horizontal
		{
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		break;
	case 20:
		if (y > 345) //stop background scroll - vertical
		{
			//scroll horizontal directions
			scrCtrl = 'h';
			scrollControl();
			
		}
		else if (y < 90) //stop background scroll - vertical
		{
			//scroll vertical directions
			scrCtrl = 'h';
			scrollControl();
			
		}
		else if (x <= 850) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		else if((x <= 1750) && (x > 850))//scroll
		{
			//scroll both directions
			scrCtrl = 'b';
			scrollControl();
			
		}
		else if(x > 1750) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		
		break;
	case 21:
		if (y > 345) //stop background scroll - vertical
		{
			//scroll horizontal directions
			scrCtrl = 'h';
			scrollControl();
			
		}
		else if (y < 90) //stop background scroll - vertical
		{
			//scroll horizontal directions
			scrCtrl = 'h';
			scrollControl();
			
		}
		else if (x <= 950) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		else if((x <= 1850) && (x > 950))//scroll
		{
			//scroll both directions
			scrCtrl = 'b';
			scrollControl();
			
		}
		else if(x > 1850) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		
		break;
	case 3:
		/*if (y < 360) //stop background scroll - vertical AND horizontal
		{
			spriteX = x - oldX;
			spriteY = y - oldY;
			
			
			PA_SetSpriteXY(0, avSprNo, spriteX + 150 , spriteY + sprStartY);	// set sprite position
			PA_LargeScrollXY(0, 1, oldX - 140, oldY - 360);
			PA_LargeScrollXY(0, 2, oldX - 140, oldY - 360);
		}
		if (x < 150) //stop background scroll - horizontal
		{
			spriteX = x - oldX;
			spriteY = y - oldY;
			
			
			PA_SetSpriteXY(0, avSprNo, spriteX + 150 , spriteY + sprStartY);	// set sprite position
			PA_LargeScrollXY(0, 1, oldX - 140, y - 360);
			PA_LargeScrollXY(0, 2, oldX - 140, y - 360);
			oldY = y;
		}
		if((x < 950) && (x >= 150))
		{
			PA_LargeScrollXY(0, 1, x - 140, y - 360);
			PA_LargeScrollXY(0, 2, x - 140, y - 360);
			oldX = x;
			oldY = y;
			
			spriteX = x - oldX;
			spriteY = y - oldY;
		}
		else if(x >= 950) //stop background scroll - horizontal
		{
			spriteX = x - oldX;
			spriteY = y - oldY;
			
			
			PA_SetSpriteXY(0, avSprNo, spriteX + 150 , spriteY + sprStartY);	// set sprite position
			PA_LargeScrollXY(0, 1, oldX - 140, y - 360);
			PA_LargeScrollXY(0, 2, oldX - 140, y - 360);
			oldY = y;
		}*/
		break;
	case 4:
		break;
	case 50:
		if ((y > 620) && (x <= 1850)) //stop background scroll - vertical
		{
			//no scroll
			scrCtrl = 'n';
			scrollControl();
		
		}	
		else if ((y > 620) && (x <= 2330) && (x > 1850)) //stop background scroll - vertical
		{
			//scroll horizontal directions
			scrCtrl = 'h';
			scrollControl();
			
		}
		else if ((y > 620) && (x > 2330)) //stop background scroll - vertical AND horizontal
		{
			//no scroll
			scrCtrl = 'n';
			scrollControl();
			
		}
		else if((x <= 2330) && (y <= 620))
		{
			//scroll both directions
			scrCtrl = 'b';
			scrollControl();
		}
		else if((x > 2330) && (y <= 620)) //stop background scroll - horizontal
		{	
			//scroll vertical directions
			scrCtrl = 'v';
			scrollControl();
		}
		break;
	}
}


void loadMap()
{
	switch(map)
	{
	case 10:
		if(x >= 860)
		{
			x = 860;
		}
		/*if(x >= 850)
		{
			spriteY = y - oldY;
			sprStartX = 20;
			
			PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap02");
			PA_LargeScrollXY(0, 1, 0, y); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap02);
			PA_LargeScrollXY(0, 2, 0, y); 
			
			//x = oldX = 0;
			
			map = 20;
			//sprStartX = 20;
			scrollOffsetX = 850;
			scrollOffsetY = 0;
		}*/
		break;
	case 11:
		if(x >= 900)
		{
		
			spriteY = y - oldY;
			sprStartX = 20;
			
			PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap02");
			PA_LargeScrollXY(0, 1, 0, y); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap02);
			PA_LargeScrollXY(0, 2, 0, y); 
			
			//x = oldX = 0;
			
			map = 20;
			//sprStartX = 20;
			scrollOffsetX = 850;
			scrollOffsetY = 0;
		}
		break;
	case 20:
		if(x < 850)
			{
				scrollOffsetX = 120;
				scrollOffsetY = 0;
				sprStartX = 150;
				spriteY = y - oldY;
				
				PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
				
				//Note loading a new map automatically deletes the previous one
				PA_ResetBgSys();
				PA_FatEasyBgLoad(0, 1, "environmentMap01");
				PA_LargeScrollXY(0, 1, x - scrollOffsetX, y); //adjust location (vertical only)
				//load new collision map
				PA_EasyBgLoad(0, 2, collisionMap01);
				PA_LargeScrollXY(0, 2, x - scrollOffsetX, y); 
				
				//x = oldX = 0;
				
				map = 11;
			}
		else if(x > 1850)
		{
			spriteY = y - oldY;
			scrollOffsetY = 120;
			
			PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap05");
			PA_LargeScrollXY(0, 1, 0, y - scrollOffsetY); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap05);
			PA_LargeScrollXY(0, 2, 0, y - scrollOffsetY); 
			
			//x = oldX = 0;
			
			map = 50;
			//sprStartX = 20;
			scrollOffsetX = 1850;
			//scrollOffsetY = 120;
		}
		/*else if(y > 360)
		{
			spriteX = x - oldX;
			
			PA_SetSpriteXY(0, avSprNo, spriteX + 150, 20);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap03");
			PA_LargeScrollXY(0, 1, x - 140, y - 360); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap03);
			PA_LargeScrollXY(0, 2, x - 140, y - 360); 
			
			map = 3;
		}*/
		break;
	case 21:
		if(x < 850)
			{
			
				scrollOffsetX = 120;
				scrollOffsetY = 0;
				sprStartX = 150;
				spriteY = y - oldY;
				
				PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
				
				//Note loading a new map automatically deletes the previous one
				PA_ResetBgSys();
				PA_FatEasyBgLoad(0, 1, "environmentMap01");
				PA_LargeScrollXY(0, 1, x - scrollOffsetX, y); //adjust location (vertical only)
				//load new collision map
				PA_EasyBgLoad(0, 2, collisionMap01);
				PA_LargeScrollXY(0, 2, x - scrollOffsetX, y); 
				
				//x = oldX = 0;
				
				map = 11;
			}
		else if(x > 1850)
		{
			spriteY = y - oldY;
			scrollOffsetY = 120;
			
			PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap05");
			PA_LargeScrollXY(0, 1, 0, y - scrollOffsetY); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap05);
			PA_LargeScrollXY(0, 2, 0, y - scrollOffsetY); 
			
			//x = oldX = 0;
			
			map = 50;
			//sprStartX = 20;
			scrollOffsetX = 1850;
			//scrollOffsetY = 120;
		}
		break;
	case 3:
		break;
	case 4:
		break;
	case 50:
	if((x <= 1850) && (y < 500))
		{
			spriteY = y - oldY;
			
			scrollOffsetX = 950;
			scrollOffsetY = 0;
			sprStartX = 150;
			
			PA_SetSpriteXY(0, avSprNo, sprStartX, spriteY + sprStartY);	// set sprite position
			
			//Note loading a new map automatically deletes the previous one
			PA_ResetBgSys();
			PA_FatEasyBgLoad(0, 1, "environmentMap02");
			PA_LargeScrollXY(0, 1, x - scrollOffsetX, y); //adjust location (vertical only)
			//load new collision map
			PA_EasyBgLoad(0, 2, collisionMap02);
			PA_LargeScrollXY(0, 2, x - scrollOffsetX, y); 
			
			//x = oldX = 0;
			
			map = 21;
		}
		break;
	}
}

	


#endif  //__SEAMLESSMAP_H__
